package com.bj58.heatmap.db;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.bj58.groupbuy.util.PathUtil;
import com.bj58.heatmap.model.DispLocal;

public class CityDao {

	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	public static DispLocal getDispLocalById(int local_id) throws SQLException {
		DispLocal dispLocal = null;
		Connection con = getConnection();
		Statement st = null;
		ResultSet rs = null;
		String sql = "select city_id,city_name,city_name_en from m_common_disp_city where city_id="
				+ local_id + ";";
		try {
			st = con.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				dispLocal = new DispLocal();
				dispLocal.setCity_id(local_id);
				dispLocal.setCity_name(rs.getString("city_name"));
				dispLocal.setCity_name_en(rs.getString("city_name_en"));
			}
			
			if (rs != null)
				rs.close();
			if (st != null)
				st.close();
			if (con != null)
				con.close();
			rs = null;
			st = null;
			con = null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return dispLocal;
	}

	public static List<DispLocal> getDispLocalList() {
		List<DispLocal> localList = new ArrayList<DispLocal>();

		try {
			Connection con = getConnection();
			Statement st = null;
			ResultSet rs = null;
			String sql = "select city_id,city_name,city_name_en from m_common_disp_city where city2_name is null;";
			st = con.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				DispLocal dispLocal = new DispLocal();
				dispLocal.setCity_id(rs.getInt("city_id"));
				dispLocal.setCity_name(rs.getString("city_name"));
				dispLocal.setCity_name_en(rs.getString("city_name_en"));
				localList.add(dispLocal);
			}
			if (rs != null)
				rs.close();
			if (st != null)
				st.close();
			if (con != null)
				con.close();
			rs = null;
			st = null;
			con = null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return localList;
	}

	/**
	 * 创建一个新的数据库连接池
	 * 
	 * @param name
	 *            JNDI的搜索名
	 * @param record
	 *            数据库连接池的基本信息
	 * @param constants
	 *            系统内置的环境管理类
	 * @return 新的数据库连接池，null表示创建失败
	 */
	private static Connection getConnection() {
		Connection conn=null;
		try {
			File propfile = null;
			String path = PathUtil.getWFNamespaceFolder()
						+ "/db.properties";
			propfile = new File(path);
			Properties p = new Properties();
			FileInputStream fis = new FileInputStream(propfile);
			p.load(fis);
			fis.close();
			Class.forName(p.getProperty("DriversClass"));
			String url = p.getProperty("ConnetionURL");
			String user= p.getProperty("UserName");
			String password= p.getProperty("PassWord");
			conn=DriverManager.getConnection(url, user, password);
			// log.debug("DbPool: 创建数据库连接池：" + name);
		} catch (Exception e) {
			e.printStackTrace();
			// log.error("DbPool: 创建数据源 " + name + " 失败：",e);
		}
		return conn;
	}
}
